import React from 'react';
import { Route, RouteProps, useLocation } from 'react-router-dom';

export default function KeepAlive({ path, component: Component }: IKeepAliveProps) {
  const location = useLocation();
  if (!Component) return null;
  return (
    <Route
      render={(_props) => {
        return (
          <div
            style={{
              display: location.pathname.startsWith(path) ? 'block' : 'none',
            }}
          >
            <Component {..._props} />
          </div>
        );
      }}
    />
  );
}
interface IKeepAliveProps extends RouteProps {
  path: string;
}
